package com.woniuxy.yoga.product.service.job;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.yoga.business.dao.model.BusinessStore;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.BusinessState;
import com.woniuxy.yoga.commons.core.statedata.CoachState;
import com.woniuxy.yoga.commons.core.statedata.ProductState;
import com.woniuxy.yoga.commons.core.util.DateUtil;
import com.woniuxy.yoga.product.client.BusinessStoreClient;
import com.woniuxy.yoga.product.client.CoachClient;
import com.woniuxy.yoga.product.client.dto.business.BusinessStoreDTO;
import com.woniuxy.yoga.product.client.dto.coach.CoachByIdDTO;
import com.woniuxy.yoga.product.dao.mapper.TeamProductMapper;
import com.woniuxy.yoga.product.dao.model.TeamProduct;
import com.woniuxy.yoga.product.model.exception.ProductException;
import com.woniuxy.yoga.product.model.exception.ProductExceptionCode;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class TeamProductExpireJob {

    @Autowired
    private TeamProductMapper teamProductMapper;

    @Autowired
    private BusinessStoreClient businessStoreClient;

    @Autowired
    private CoachClient coachClient;

    @XxlJob("teamProductExpire")
    @GlobalTransactional(rollbackFor = Exception.class)
    public void TeamProductExpire(){
        System.out.println("团课开课");
        String productId = XxlJobHelper.getJobParam();
        JSONObject jsonObject = JSONObject.parseObject(productId);
        Map<String, Object> map = JSONObject.toJavaObject(jsonObject, Map.class);
        //获取团队产品ID
        Integer teamProductId = (Integer) map.get("teamProductId");
        TeamProduct teamProduct = teamProductMapper.selectById(teamProductId);
        //判断是否存在团队产品
        if (teamProduct==null) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_TEAM_PRODUCT_NOT_EXIST);
        //判断团队产品状态是否异常
        if (teamProduct.getTeamProductState()!= ProductState.TEAM_PRODUCT_STATE_WAIT) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_TEAM_PRODUCT_STATE_ERROR);
        //判断是否报错
        Result<BusinessStoreDTO> businessStoreResult = businessStoreClient.selByBusinessStoreId(teamProduct.getStoreId());
        if (businessStoreResult.getCode()!=200) throw new ProductException(businessStoreResult.getCode(),businessStoreResult.getMessage());
        //获取数据
        BusinessStoreDTO businessStore = businessStoreResult.getData();
        //判断场馆是否存在
        if (businessStore==null) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_STORE_ABSENT);
        //判断场馆状态是否异常
        if (businessStore.getStoreState()!= BusinessState.STORE_STATE_NORMAL) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_STORE_STATE_ERROR);
        //判断教练是否报错
        Result<CoachByIdDTO> coachByIdDTOResult = coachClient.getCoachById(teamProduct.getCoachId());
        if (coachByIdDTOResult.getCode()!=200) throw new ProductException(coachByIdDTOResult.getCode(),coachByIdDTOResult.getMessage());
        //获取数据
        CoachByIdDTO coachByIdDTO = coachByIdDTOResult.getData();
        //判断是否存在
        if (coachByIdDTO==null) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_COACH_NOT_EXIST);
        //判断教练状态是否异常
        if (coachByIdDTO.getCoachState()!= CoachState.COACH_STATE_NORMAL) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_COACH_STATE_ERROR);
        //判断当前时间是否符合开课时间
        String openTime = teamProduct.getOpenTime();
        //将开课时间转毫秒
        long open = DateUtil.convertToMilliseconds(openTime);
        long time = new Date().getTime();
        //判断当前时间是否大于等于开课时间
        if (time>=open){
            //判断已预约人数是否大于等于开课人数
            if (teamProduct.getBookNum()>=teamProduct.getOpenNum()){
                //判断状态是否是待开课
                if (teamProduct.getTeamProductState()==ProductState.TEAM_PRODUCT_STATE_WAIT){
                    //将团队产品状态修改成已开课
                    teamProduct.setTeamProductState(ProductState.TEAM_PRODUCT_STATE_START);
                    teamProductMapper.updateById(teamProduct);
                }
            }
        }
    }
}
